package com.yhy.wxzn.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhy.wxzn.common.mapper.WmsBusinessMapper;
import com.yhy.wxzn.common.service.IWmsBusinessService;
import org.jeecg.common.system.base.entity.JeecgEntity;
import org.jeecg.common.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @Auther: 97354
 * @Date: 2023/11/1 09:45
 * @Description: WmsBusinessServiceImpl
 * @Version 1.0.0
 */
@Service
public class WmsBusinessServiceImpl extends ServiceImpl<WmsBusinessMapper, JeecgEntity> implements IWmsBusinessService {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public String genWmsBusinessCode(String prefix, int length) {
        Long nextNumber = redisUtil.hincr(prefix,prefix,1,0, TimeUnit.NANOSECONDS);
        String format = String.format("%0" + length + "d", nextNumber);
        prefix = prefix + format;
        return prefix;
    }

    @Override
    public Integer genDeviceSerialNo(String deviceType) {
        Long nextNumber = redisUtil.hincr(deviceType,deviceType,1,0, TimeUnit.NANOSECONDS);
        if(nextNumber.intValue() == Integer.MAX_VALUE){
            redisUtil.del(deviceType);
        }
        return nextNumber.intValue();
    }
}
